package com.kebabeaters.nakebab.client.ui.widget;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.kebabeaters.nakebab.client.event.CircleMouseOverEvent;
import com.kebabeaters.nakebab.client.event.CircleMouseOverEvent.Handler;
import com.kebabeaters.nakebab.client.event.CircleMouseOverEvent.HasCircleMouseOverHandler;
import com.kebabeaters.nakebab.client.resources.ResourcesFactory;
import com.kebabeaters.nakebab.client.resources.client.KebabResources.MenuStyle;

public class CircleSubMenu extends CircleLayoutPanel<CircleSubMenu> implements
		HasCircleMouseOverHandler {

	private MenuStyle style;
	private FlowPanel selection;

	public CircleSubMenu(int radius, int range, int radiusOffset) {
		super(radius, range, radiusOffset);

		style = ResourcesFactory.getKebabResources().menuStyle();
		style.ensureInjected();

		selection = new FlowPanel();
		selection.getElement().setAttribute("style",
				"border-radius: " + radius + "px;");
		selection.setWidth(radius + "px");
		selection.setHeight(radius + "px");
		selection.getElement().getStyle().setBackgroundColor("#3060D1");
		selection.getElement().getStyle().setMarginLeft(-1, Unit.PX);
		selection.getElement().getStyle().setMarginTop(-1, Unit.PX);
		selection.getElement().getStyle().setPaddingBottom(2, Unit.PX);
		selection.getElement().getStyle().setPaddingRight(2, Unit.PX);
		
		selection.getElement().getStyle().setOpacity(0);

		circle.setWidget(selection);

		initHandlers();
	}

	public FocusPanel getFocusPanel() {
		return circle;
	}
	
	public void setSelection(double progress) {
		selection.getElement().getStyle().setOpacity(progress);
	}

	private void initHandlers() {
		circle.addMouseOverHandler(new MouseOverHandler() {

			@Override
			public void onMouseOver(MouseOverEvent event) {
				fireEvent(new CircleMouseOverEvent(true));
			}
		});
		
		circle.addMouseOutHandler(new MouseOutHandler() {
			
			@Override
			public void onMouseOut(MouseOutEvent event) {
				fireEvent(new CircleMouseOverEvent(false));
			}
		});
	}

	@Override
	public HandlerRegistration addCircleMouseOverHandler(Handler handler) {
		return addHandler(handler, CircleMouseOverEvent.getType());
	}

	
}
